package Stana;
import java.awt.Color;
import java.awt.Frame;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.text.FieldView;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.linuxense.javadbf.*;

public class JavaDBFReaderTest
{ 
	public static double number, answer;
	static long ldate;
	long ndate;
	static long datediff;
	static long datediffofdays;
	public static double totalcityblock,tpricecityblock,tqtycityblock,tdatecityblock;
	public static double totaleuclidean,tpriceeuclidean,tqtyeuclidean,tdateeuclidean;
	public static Vector vectorquantity;
	public static Vector vectorprc;
	public static Vector vectordte;
	public JavaDBFReaderTest()
	{

		double totalday = 0,totalqty=0,totalprice=0;
		double mdate=0,mqty=0,mprice=0;
		double vectordate=0,vectorprice=0,vectorqty=0,res1=0,res2=0,res3=0;
		Object []rowObjects;
		Object x1=null,y1 = null,y2=null,vq=null;
		//Vector<Double> vecprice=new Vector<Double>();
		vectorquantity=new Vector();
		vectorprc=new Vector();
		vectordte=new Vector();
		Vector v1=new Vector();
		Vector v2=new Vector();
		Vector v3=new Vector();
		Vector vcityblock=new Vector();
		Vector veuclidean=new Vector();
		try {

			// create a DBFReader object
			//
			InputStream inputStream  = new FileInputStream("C:\\Challiz\\LC_TRANS.DBF"); // take dbf file as program argument
			DBFReader reader = new DBFReader( inputStream); 

			// get the field count if you want for some reasons like the following
			//
			int numberOfFields = reader.getFieldCount();

			// use this count to fetch all field information
			// if required
			//

			for( int i=0; i<numberOfFields; i++) {

				DBFField field = reader.getField( i);

				// do something with it if you want
				// refer the JavaDoc API reference for more details
				System.out.println(field.getName());

			}

			// Now, lets us start reading the rows
			//

			while( (rowObjects = reader.nextRecord()) != null) {

				for( int i=0; i<rowObjects.length; i++)
				{

					x1=rowObjects[1];
					y1=rowObjects[5];

					y2=rowObjects[6];

					System.out.println(" rowObjects " + rowObjects[1].toString())  ;
					System.out.println(" row Object 2 " + rowObjects[5].toString());
					System.out.println( " Row object 5 " + rowObjects[6].toString());

					Date lastdate,earlydate;
					earlydate=(Date)x1;


					long ndate=earlydate.getTime();
					datediff=ldate-ndate;
					datediffofdays=datediff / (24 * 60 * 60 * 1000);
					lastdate=(Date)x1;
					ldate=lastdate.getTime();
					totalday+=datediffofdays;
					totalqty+=Double.valueOf(y1.toString()).doubleValue();
					totalprice+=Double.valueOf(y2.toString()).doubleValue();
				} 
				v1.addElement(datediffofdays); 
				v2.addElement(y1);
				v3.addElement(y2);
			}

			mdate=totalday/1147;
			mqty=totalqty/1147;
			mprice=totalprice/1147;

			System.out.println("Date"+mdate);
			System.out.println("quantity"+mqty);
			System.out.println("price"+mprice);


			for(int i=0;i<v2.size();i++)
			{
				System.out.println(v1.get(i));
				vectordate=(Long)v1.get(i);
				vectorqty=(Double)v2.get(i);
				vectorprice=(Double)v3.get(i);
				if(vectordate<0)
				{

					vectordate *=-1;

				}
				res1=mdate-vectordate;
				vectordte.add(res1);
				if (vectorqty < 0)
				{
					vectorqty *=-1;


				}
				res2=mqty-vectorqty;
				vectorquantity.add(res2);

				if (vectorprice < 0)
				{
					vectorprice *=-1;
				}
				res3=mprice-vectorprice;
				vectorprc.add(res3);

			}
			for(int i=0;i<vectorquantity.size();i++)
			{
				double a=(Double)vectordte.get(i);
				double b=(Double)vectorquantity.get(i);
				double c=(Double)vectorprc.get(i);
				tdatecityblock+=a;
				tqtycityblock+=b;
				tpricecityblock+=c;
				double cityblock=a+b+c;
				double square=(a*a)+(b*b)+(c*c);
				tdateeuclidean+=Math.sqrt(a*a);
				tqtyeuclidean+=Math.sqrt(b*b);
				tpriceeuclidean+=Math.sqrt(c*c);
				double euclidean=Math.sqrt(square);
				vcityblock.addElement(cityblock);
				veuclidean.addElement(euclidean);


				// System.out.println("CityBlock Distance is:-->"+cityblock);

				//System.out.println("Euclidean Distance is:-->"+Math.sqrt(square));
			}
			for(int i=0;i<vcityblock.size();i++)
			{
				totalcityblock+=(Double)vcityblock.get(i);
				totaleuclidean+=(Double)veuclidean.get(i);
				//System.out.println(vcityblock.get(i));
				//System.out.println(veuclidean.get(i));
			}
			System.out.println("CityBlock Distance isss:-->"+tpricecityblock);

			System.out.println("Euclidean Distance isss:-->"+tqtyeuclidean);
			inputStream.close();

		}
		catch( DBFException e) {

			System.out.println( e.getMessage());
		}
		catch( IOException e) {

			System.out.println( e.getMessage());
		}
		//  addButton();
	} 
	public static void main(String[] args) {
		new JavaDBFReaderTest();
	}
	public static void addButton()
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.setValue(answer, "Profit1", "Jan");
		dataset.setValue(3, "Profit2", "Jane");
		dataset.setValue(7, "Profit1", "Tom");
		dataset.setValue(10, "Profit2", "Tom");
		dataset.setValue(8, "Profit1", "Jill");
		dataset.setValue(8, "Profit2", "Jill");
		dataset.setValue(5, "Profit1", "John");
		dataset.setValue(6, "Profit2", "John");
		dataset.setValue(12, "Profit1", "Fred");
		dataset.setValue(5, "Profit2", "Fred");
		// Profit1, Profit2 represent the row keys
		// Jane, Tom, Jill, etc. represent the column keys
		JFreeChart chart = ChartFactory.createBarChart3D( "Comparison between Salesman",
				"Salesman", "Value ($)", dataset, PlotOrientation.VERTICAL, true, true, false );	
		chart.setBackgroundPaint(Color.yellow); // Set the background colour of the chart
		chart.getTitle().setPaint(Color.blue); // Adjust the colour of the title
		CategoryPlot p = chart.getCategoryPlot(); // Get the Plot object for a bar graph
		p.setBackgroundPaint(Color.black); // Modify the plot background
		p.setRangeGridlinePaint(Color.red);
		ChartFrame fr=new ChartFrame("BarChart", chart);
		fr.setSize(400,400);
		fr.setVisible(true);

	}
}  